package com.zt.student.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.Do.ClassWithTeachDO;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.utils.ClassUtil;
import com.zt.common.utils.JwtUtil;
import com.zt.common.utils.RedisUtil;
import com.zt.student.dao.ActiveClassDao;
import com.zt.student.dto.ClassScoreDTO;
import com.zt.student.service.ActiveClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-26
 */

@Service
@Transactional
public class ActiveClassServiceImpl implements ActiveClassService {

    private static final Log log = LogFactory.get();

    @Autowired
    private ActiveClassDao activeClassDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ActiveClass getClassById(int id) {
        return activeClassDao.getClassById(id);
    }

    /**
     * @param semester 选课学期
     * @return 该学生可选的所有通选选修课
     */
    @Override
    public List<ClassWithTeachDO> getAllActiveClass(String semester, HttpServletRequest request) {
        //获取当前登录学生id
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        log.info("当前登录学生id：" + studentId + "，获取该学生" + semester + "学期所有可选课堂");

        return activeClassDao.getAllActiveClass(studentId,semester);
    }

    /**
     * @param semester 选课学期
     * @return 获取除去与学生已选课堂时间冲突外的可选通选选修课堂
     */
    @Override
    public List<ClassWithTeachDO> getOptionalActiveClass(String semester, HttpServletRequest request){
        //获取当前登录学生id
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        log.info("当前登录学生id：" + studentId + "，获取该学生" + semester + "学期过滤时间冲突后的可选课堂");

        //已选择的课程
        List<ClassWithTeachDO> selectedClass = activeClassDao.getAllSelectedClass(studentId, semester);
        //获取已选择的课程的所有上课时间
        String classTime = ClassUtil.getClassTime2(selectedClass);
        List<ClassWithTeachDO> allClass = activeClassDao.getAllActiveClass(studentId, semester);

        List<ClassWithTeachDO> optionalClass = new ArrayList<>();
        for (ClassWithTeachDO activeClass : allClass) {
            String time = activeClass.getClassTime();
            if (!ClassUtil.isTimeConflict(classTime,time)) optionalClass.add(activeClass);
        }

        return optionalClass;
    }

    @Override
    public List<ClassWithTeachDO> getAllSelectedClass(String semester, HttpServletRequest request) {
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        log.info("当前登录学生id：" + studentId + "，获取该学生" + semester + "学期已选全部课堂");
        return activeClassDao.getAllSelectedClass(studentId,semester);
    }

    @Override
    public List<ClassWithTeachDO> getSelectedElectiveClass(String semester, HttpServletRequest request) {
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        log.info("当前登录学生id：" + studentId + "，获取该学生" + semester + "学期已选选修、通选课堂");

        return activeClassDao.getSelectedElectiveClass(studentId,semester);
    }

    @Override
    public List<ClassWithTeachDO> getSelectedCompulsoryClass(String semester, HttpServletRequest request) {
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        log.info("当前登录学生id：" + studentId + "，获取该学生" + semester + "学期已选必修课堂");

        return activeClassDao.getSelectedCompulsoryClass(studentId,semester);
    }

    /**
     * @param semester 学期
     * @param request  获取学生id
     * @return  按一周七天返回该学生的课程表情况
     */
    @Override
    public List<List<ClassWithTeachDO>> getSchedule(String semester,HttpServletRequest request){
        if (redisUtil.hasKey(semester + "-selectClass") || redisUtil.hasKey(semester + "-startClass"))
            throw new ApiException(CodeMsg.SELECT_CLASS_NOT_OVER);

        List<List<ClassWithTeachDO>> res = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            res.add(new ArrayList<>());
        }
        //获取当前学生所有已选课堂
        List<ClassWithTeachDO> allSelectedClass = getAllSelectedClass(semester, request);
        for (ClassWithTeachDO activeClass : allSelectedClass) {
            //将该节课的上课时间切分为7天
            String[] days = StrUtil.cut(activeClass.getClassTime(), 15);
            for (int i = 0; i < days.length; i++) {
                if (days[i].contains("1")){
                    ClassWithTeachDO classInList = activeClass.clone();
                    //将数组中的该节课的上课时间由原来的一周上课时间7*15修改为当天的上课时间15
                    classInList.setClassTime(days[i]);
                    List<ClassWithTeachDO> list = res.get(i);
                    list.add(classInList);
                }
            }
        }
        return res;
    }

    @Override
    public List<ActiveClass> getClassByTeaAndStuId(HttpServletRequest request,String teacherId, String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        return activeClassDao.getClassByTeaAndStuId(teacherId,studentId,semester);
    }

    /**
     * @return 根据学期获取课程名字与分数
     */
    @Override
    public List<ClassScoreDTO> getClassScore(HttpServletRequest request, String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        return activeClassDao.getScoreBySemester(semester,studentId);
    }
}
